package com.believexin.dimdate;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

@SpringBootApplication
public class DimDateApplication {

    private static final RestTemplate restTemplate = new RestTemplate();

    private static final String URL = "https://api.apihubs.cn/holiday/get?field=workday,date,holiday_legal,week,month,year,yearweek,yearday,holiday_today&year=%s&cn=1&size=366";

    public static void main(String[] args) {
        String year = System.getProperty("year");
        String path = System.getProperty("path");
        ResponseEntity<ResponseType> res = restTemplate.getForEntity(String.format(URL, year), ResponseType.class);


        File file = new File(path + File.separator + "dim_data_" + year + ".txt");
        try (FileWriter fw = new FileWriter(file);) {
            ResponseType body = res.getBody();
            for (DataSet datum : body.getData().getList()) {
                StringBuilder builder = new StringBuilder();
                builder.append(datum.getYear()).append(" ").append(datum.getYear_cn()).append(" ");
                builder.append(datum.getMonth()).append(" ").append(datum.getMonth_cn()).append(" ");
                builder.append(datum.getDate()).append(" ").append(datum.getDate_cn()).append(" ");
                builder.append(datum.getYearweek()).append(" ").append(datum.getYearweek_cn()).append(" ");
                builder.append(datum.getYearday()).append(" ").append(datum.getYearday_cn()).append(" ");
                builder.append(datum.getWeek()).append(" ").append(datum.getWeek_cn()).append(" ");
                builder.append(datum.getWorkday()).append(" ").append(datum.getWorkday_cn()).append(" ");
                builder.append(datum.getHoliday_today()).append(" ").append(datum.getHoliday_today_cn()).append(" ");
                builder.append(datum.getHoliday_legal()).append(" ").append(datum.getHoliday_legal_cn());
                builder.append("\r\n");
                fw.write(builder.toString());
                fw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
